package com.atwisdom.star.core.bsl.planner.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.DateUtils;
import com.atwisdom.star.core.bsl.planner.SysConfigBslService;
import com.atwisdom.star.core.dao.pojo.modelClass.staticLogic.StaticLogicInstance;
import com.atwisdom.star.core.dsl.modelClass.staticLogic.Impl.StaticLogicInstanceDslServiceImpl;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Data
@NoArgsConstructor
public class SysConfigBslServiceImpl implements SysConfigBslService {
    private StaticLogicInstanceDslServiceImpl staticLogicInstanceDslService;

    @Autowired
    public void setStaticLogicInstanceDslService(StaticLogicInstanceDslServiceImpl staticLogicInstanceDslService) {
        this.staticLogicInstanceDslService = staticLogicInstanceDslService;
    }

    //标准工时
    private int workHours;
    private String workStartTime;
    private String workEndTime;
    private LocalDateTime workEndDateTime;
    private LocalDateTime workStartDateTime;

    /**
     * 获取标准工时
     */
    public void initSysConfig() {
        List<StaticLogicInstance> instances = this.staticLogicInstanceDslService.queryByClassName("SysConfig");
        List<JSONObject> jsonObjectList = this.staticLogicInstanceDslService.getStaticLogicInsJsonObjectList(instances);
        if (CollectionUtil.isNotEmpty(jsonObjectList)) {
            JSONObject jsonObject = jsonObjectList.get(0);
            JSONObject insValues = jsonObject.getJSONObject("values");
            this.workHours = insValues.getInteger("workHours");
            this.workStartTime = insValues.getString("workStartTime");
            this.workEndTime = insValues.getString("workEndTime");
        }
    }

    public LocalDateTime getWorkStartDateTime(LocalDate date){
        return DateUtils.getLocalDateTime(date,workStartTime);
    }

    public LocalDateTime getWorkEndDateTime(LocalDate date){
        return DateUtils.getLocalDateTime(date,workEndTime);
    }
}
